<?php

function add_service($svr, $method, &$con) {
	$arr = explode('.', $svr. ".". $method);
	$parent_id = 0;

	foreach($arr as $v) {
		$is_folder = 1;
		if($v==$method) {
			$is_folder = 0;
		}

		$sql = "SELECT id FROM dev_db.fsof_case_tree WHERE name='$v' AND is_folder=$is_folder AND parent_id=$parent_id";
		$result = mysql_query($sql, $con);
		$row = mysql_fetch_array($result,MYSQL_ASSOC);              

		if($row !=false && count($row)>0) {
			$parent_id = $row['id'];
		}else {
			$i_sql = "INSERT INTO dev_db.fsof_case_tree SET name='$v', is_folder=$is_folder, parent_id=$parent_id, module_id=21";
			echo $i_sql . PHP_EOL;
			$result = mysql_query($i_sql,$con);

			$s_sql = "SELECT MAX(id) as id FROM dev_db.fsof_case_tree";
			$result = mysql_query($s_sql,$con);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);              
			$parent_id = $row['id'];
		}
	}

	$node_id = intval($parent_id);
	return $node_id;
}

function get_out_dump($src) {
	$handle = fopen($src, 'r');

	$out = array();
	$preg = '/->(\d+\.\d+\.\d+\.\d+):(\d+)\|\|(.+)/';
	while(!feof($handle)) {
		$line = fgets($handle);
		$matches = array();
		preg_match($preg, $line, $matches);
		if(!isset($matches[1]) || !isset($matches[2]) || !isset($matches[3])) {
			continue ;
		}
		
		$ip = $matches[1];
		$port = $matches[2];
		$ret = $matches[3];

		$obj = json_decode($ret, true);
		if(!is_array($obj)) {
			continue;
		}
		
		$svr = $obj['service'];
		$method = $obj['method'];

		if(array_key_exists($svr, $out) && 
			isset($out[$svr][$method]) && 
			isset($out[$svr][$method]['req']) && 
			isset($out[$svr][$method]['resp'])) {
			continue ;
		}

		if(array_key_exists('result', $obj) && !isset($out[$svr][$method]['resp'])) {
			$out[$svr][$method]['resp'] = $ret;	
		}else if(!isset($out[$svr][$method]['req'])) {
			$out[$svr][$method]['req'] = $ret;
			$out[$svr][$method]['ip'] = $ip;	
			$out[$svr][$method]['port'] = $port;	
		}
	}

	return $out;
}

function main($out, &$con) {
	foreach ($out as $key => $value) {
		$srv = $key;
		foreach ($value as $k => $v) {
			$method = $k;
			$node_id = add_service($srv, $method, $con);
			if($node_id<=0){
				return ;
			}

			$req = isset($v['req'])? $v['req']: "";
			$resp = isset($v['resp'])? $v['resp']: "";
			$ip = isset($v['ip'])? $v['ip']: "";
			$port = isset($v['port'])? $v['port']: "";

			$sql = "SELECT id FROM dev_db.fsof_case_content WHERE node_id=$node_id";
			$result = mysql_query($sql,$con);
			$row = mysql_fetch_array($result,MYSQL_ASSOC);              
			if($row !=false && count($row)>0) {
				continue;	
			}

			$sql = "INSERT INTO dev_db.fsof_case_content SET node_id=$node_id, request='$req', response='$resp',ip='$ip', port=$port, author='author'";
			echo $sql .PHP_EOL;
			$result = mysql_query($sql,$con);
		}
	}
}


$con = mysql_connect("10.1.48.26","root","fenqile@123");
mysql_query("SET NAMES UTF8");
$src = $argv[1];
$out = get_out_dump($src);
//$in = array('com.fenqile.user.user.IUserQueryService'=>$out['com.fenqile.user.user.IUserQueryService']);
//var_dump($in);
main($out, $con);
//var_dump($out);
//var_dump($out['com.fenqile.user.user.IUserQueryService']);





